/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

/*
 * NewJPanel.java
 *
 * Created on 18.03.2012, 12:05:12
 */

package org.opu.pcm.ui.client.widgets;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.opu.pc.domain.dao.PublicationDao;
import org.opu.pc.domain.dao.PublisherDao;
import org.opu.pc.ui.client.MainViewImpl;
import org.opu.pc.ui.client.actions.License;
import org.opu.pc.ui.client.i18n.AbstractI18N;
import org.opu.pc.ui.client.widgets.PropertiesEditPane;
import org.opu.pc.ui.client.widgets.PropertiesEditPane.PropertyOption;
import org.opu.pc.ui.client.widgets.TabelBeanView;
import org.opu.pc.ui.client.widgets.TableRender;
import org.opu.pc.ui.util.GUIUtils;
import org.opu.pc.ui.util.NullableProperties;
import org.opu.pc.ui.util.ResourceHandle;

/**
 *
 * @author yura
 */
public class NewJPanel extends javax.swing.JPanel {

    /** Creates new form NewJPanel */
    public NewJPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label = new javax.swing.JLabel();
        databaseBtn = new javax.swing.JButton();
        userManagementBtn = new javax.swing.JButton();
        licenseBtn = new javax.swing.JButton();
        exiteBtn = new javax.swing.JButton();
        helpBtn = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(580, 400));

        label.setFont(new java.awt.Font("SansSerif", 1, 24));
        label.setText("Select a category");

        databaseBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/opu/pcm/resource/Database60.png"))); // NOI18N
        databaseBtn.setText("Database sets");
        databaseBtn.setToolTipText("Database connection properties");
        databaseBtn.setBorderPainted(false);
        databaseBtn.setContentAreaFilled(false);
        databaseBtn.setFocusPainted(false);
        databaseBtn.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        databaseBtn.addMouseListener(org.opu.pc.ui.client.MainToolBar.MOUSE_LISTENER);
        databaseBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DatabaseAction(evt);
            }
        });

        userManagementBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/opu/pcm/resource/badIcon.png"))); // NOI18N
        userManagementBtn.setText("User management");
        userManagementBtn.setBorderPainted(false);
        userManagementBtn.setContentAreaFilled(false);
        userManagementBtn.setEnabled(false);
        userManagementBtn.setFocusPainted(false);
        userManagementBtn.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        userManagementBtn.addMouseListener(org.opu.pc.ui.client.MainToolBar.MOUSE_LISTENER);

        licenseBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/opu/pcm/resource/License60.png"))); // NOI18N
        licenseBtn.setText("Licenses");
        licenseBtn.setBorderPainted(false);
        licenseBtn.setContentAreaFilled(false);
        licenseBtn.setFocusPainted(false);
        licenseBtn.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        licenseBtn.addMouseListener(org.opu.pc.ui.client.MainToolBar.MOUSE_LISTENER);
        licenseBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LicensesAction(evt);
            }
        });

        exiteBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/opu/pc/ui/resource/icons/small/Exit.png"))); // NOI18N
        exiteBtn.setText("Exit");
        exiteBtn.setBorderPainted(false);
        exiteBtn.setContentAreaFilled(false);
        exiteBtn.setFocusPainted(false);
        exiteBtn.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        exiteBtn.addMouseListener(org.opu.pc.ui.client.MainToolBar.MOUSE_LISTENER);
        exiteBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExitAction(evt);
            }
        });

        helpBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/opu/pc/ui/resource/icons/small/Help.png"))); // NOI18N
        helpBtn.setText("Help");
        helpBtn.setBorderPainted(false);
        helpBtn.setContentAreaFilled(false);
        helpBtn.setFocusPainted(false);
        helpBtn.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        helpBtn.addMouseListener(org.opu.pc.ui.client.MainToolBar.MOUSE_LISTENER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(licenseBtn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(userManagementBtn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(databaseBtn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(label, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 288, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(exiteBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(helpBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label)
                .addGap(18, 18, 18)
                .addComponent(databaseBtn)
                .addGap(18, 18, 18)
                .addComponent(userManagementBtn)
                .addGap(18, 18, 18)
                .addComponent(licenseBtn)
                .addContainerGap(95, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(330, Short.MAX_VALUE)
                .addComponent(helpBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(exiteBtn)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void ExitAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExitAction
        System.exit(0);
    }//GEN-LAST:event_ExitAction

    private void LicensesAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LicensesAction
        License l = new License();
        l.setMainView(new MainViewImpl(new TabelBeanView(), new ArrayList<TableRender<?>>()));
        l.actionPerformed(evt);
    }//GEN-LAST:event_LicensesAction

    private void DatabaseAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DatabaseAction
        LinkedHashMap<String, Object> properties = new LinkedHashMap<String, Object>();
		properties.put("hibernate.dialect", "");
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.hbm2ddl.auto", "create");

        properties.put("jdbc.driverClassName", "");
        properties.put("jdbc.url", "");
        properties.put("jdbc.username", "");
        properties.put("jdbc.password", "");

		Map<String, List<PropertyOption>> map = new HashMap<String, List<PropertyOption>>();
        map.put("hibernate.dialect", Arrays.asList(
				new PropertyOption("null", "PostgreSQLDialect", "org.hibernate.dialect.PostgreSQLDialect"),
				new PropertyOption("null", "HSQLDialect", "org.hibernate.dialect.HSQLDialect")));
        map.put("hibernate.show_sql", Arrays.asList(
				new PropertyOption("null", "Yes", "true"),
				new PropertyOption("null", "No", "false")));
        map.put("hibernate.hbm2ddl.auto", Arrays.asList(
				new PropertyOption("null", "Update", "update"),
				new PropertyOption("null", "Validate", "validate"),
				new PropertyOption("null", "Create", "create"),
				new PropertyOption("null", "Drop and create", "create-drop")));
        map.put("jdbc.driverClassName", Arrays.asList(
				new PropertyOption("null", "PostgreSQL", "org.postgresql.Driver"),
				new PropertyOption("null", "HSQL database", "org.hsqldb.jdbcDriver")));

        final PropertiesEditPane pane = new PropertiesEditPane(properties,
				new AbstractI18N("org.opu.pcm.resource.i18n") {}, map);

        final JDialog dialog = new JDialog((JFrame)null, "Database properties - "
                + ResourceHandle.getApplicationTitle() + " management", true);

        JPanel cp = new JPanel(new BorderLayout());
		cp.add(pane);

		Box b = Box.createHorizontalBox();
		b.add(new JButton(GUIUtils.getAction("Ok", new GUIUtils.ActionListener() {

            @Override public void actionPerformed(ActionEvent e) throws Exception {
                Map<String, Object> prop = pane.getProperties();
                NullableProperties globalProp = NullableProperties.getInstance();
                globalProp.putAll(prop);
                org.springframework.context.support.ClassPathXmlApplicationContext context
                        = new org.springframework.context.support.ClassPathXmlApplicationContext(
                            "classpath:ui_applicationcontext.xml",
                            "classpath:datasource_applicationcontext.xml",
                            "classpath:dao_applicationcontext.xml"
//                            "classpath:sg_applicationcontext.xml",
//                            "classpath:services_applicationcontext.xml",
//                            "classpath:desktop_ui_applicationcontext.xml"
                        );
                ((PublicationDao)context.getBean("PublicationDao")).getList();
                ((PublisherDao)context.getBean("PublisherDao")).getList();
                globalProp.store();
                dialog.dispose();
            }
        })));
		b.add(Box.createHorizontalStrut(GUIUtils.DEFAULT_SPACE));
		b.add(new JButton(GUIUtils.getAction("Cancel", new GUIUtils.ActionListener() {

            @Override public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        })));
		cp.add(GUIUtils.vBorder(GUIUtils.hCenter(b)), BorderLayout.SOUTH);

        dialog.getContentPane().add(cp);
        dialog.pack();
        Dimension s = dialog.getSize();
        s.width = 500;
        dialog.setSize(s);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
    }//GEN-LAST:event_DatabaseAction


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton databaseBtn;
    private javax.swing.JButton exiteBtn;
    private javax.swing.JButton helpBtn;
    private javax.swing.JLabel label;
    private javax.swing.JButton licenseBtn;
    private javax.swing.JButton userManagementBtn;
    // End of variables declaration//GEN-END:variables

}
